﻿using System;
using System.Collections.Generic;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Extensions;
using XCI.Windows.Extensions;
using XCI.Helper;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统报表服务
    /// </summary>
    public class ReportService : BaseService<SysReport>
    {
        public static readonly ReportService Instance = new ReportService();
        protected override string RootUrl => "/api/sys/report/";

        /// <summary>
        /// 新建报表模型
        /// </summary>
        public SysReport New()
        {
            return new SysReport
            {
                Id = NewId()
            };
        }

        /// <summary>
        /// 复制报表模型
        /// </summary>
        /// <param name="original">原模型</param>
        public SysReport Copy(SysReport original)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            var model = original?.Clone();
            model.Id = NewId();
            return model;
        }

        /// <summary>
        /// 检查报表编码是否存在
        /// </summary>
        /// <param name="code">报表编码</param>
        public bool ExistByCode(string code)
        {
            return ExistByCodeCore(code);
        }

        /// <summary>
        /// 新建报表
        /// </summary>
        /// <param name="model">报表模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysReport model)
        {
            return InsertCore(model);
        }

        /// <summary>
        /// 修改报表
        /// </summary>
        /// <param name="model">报表模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Update(SysReport model)
        {
            return UpdateCore(model);
        }

        /// <summary>
        /// 删除报表
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string ids)
        {
            return DeleteByIdsCore(ids);
        }

        /// <summary>
        /// 根据报表主键获取参数对象
        /// </summary>
        /// <param name="id">报表主键</param>
        public SysReport SelectById(string id)
        {
            return SelectByIdCore(id);
        }

        /// <summary>
        /// 根据报表编码获取参数对象
        /// </summary>
        /// <param name="code">报表编码</param>
        public SysReport SelectByCode(string code)
        {
            return SelectByCodeCore(code);
        }

        /// <summary>
        /// 查询报表分页列表
        /// </summary>
        /// <param name="paged">分页参数</param>
        /// <param name="key">编码/名称关键字</param>
        public PageList<SysReport> SelectPageList(PageParam paged, string key)
        {
            var map = ToPageMap(paged).Add(SysConst.FKeyName, key);
            return SelectPageListCore(map);
        }

        /// <summary>
        /// 查询报表列表
        /// </summary>
        /// <param name="key">名称/编码关键字</param>
        public List<SysReport> SelectList(string key)
        {
            var map = Map.New.Add(SysConst.FKeyName, key);
            return SelectListCore(map);
        }

        /// <summary>
        /// 导出报表列表
        /// </summary>
        /// <param name="key">名称/编码关键字</param>
        public byte[] Export(string key)
        {
            var map = Map.New.Add(SysConst.FKeyName, key);
            return ExportCore(map);
        }

        /// <summary>
        /// 刷新服务端报表缓存
        /// </summary>
        public bool RefreshCache()
        {
            return RefreshCacheCore();
        }

        public void SetReportPrinter(SysReport report)
        {
            if (report.Code.IsNotEmpty() && SysRuntime.ReportPrinter.TryGetValue(report.Code, out string pname))
            {
                report.Printer = pname;
            }
        }

        public void SetReportPrinter(PageList<SysReport> preports)
        {
            preports.Rows.ForEach(SetReportPrinter);
        }

        public void SetReportPrinter(List<SysReport> reports)
        {
            reports.ForEach(SetReportPrinter);
        }
    }
}
